import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/exam/dto/assessment.response.dart';
import 'package:ichinesereader/exam/dto/assessment_question_record.body.dart';
import 'package:ichinesereader/exam/widgets/BGSinglePlayer.dart';
import 'package:ichinesereader/service/assessment.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';

class StudentRunningRecordController extends GetxController {
  RxInt stepTop = (-1).obs;
  AssessmentResult? assessmentResult;
  late PdfViewerController pdfViewerController;

  final AssessmentServiceProvider _service = AssessmentServiceProvider();
  String _username = '';
  String _assessmentId = '';
  RxBool isShowSubmit = false.obs;

  void loadData(String assessmentId, String username) {
    _assessmentId = assessmentId;
    _username = username;
    getAssessment(assessmentId);
  }

  Future<void> getAssessment(String assessmentId) async {
    EasyLoading.show();
    try {
      var result = await _service.getAssessment(assessmentId);
      assessmentResult = result;
      stepTop.value = 0;
    } catch (e) {
      Log.debug(e);
    } finally {
      EasyLoading.dismiss();
    }
  }

  Future<void> reportRecord() async {
    EasyLoading.show();
    try {
      _service.postQuestionRecord(QuestionRecordBody(
          assessmentId: _assessmentId,
          questionGroupId: assessmentResult?.questionGroupId ?? '',
          userName: _username,
          progressStatus: '',
          record: _buildQuestionRecordList()));
    } catch (e) {
      Log.debug(e);
    } finally {
      EasyLoading.dismiss();
    }
  }

  Future<void> submitReportRecord() async {
    EasyLoading.show();
    try {
      _service.postSubmitReportRecord(QuestionRecordBody(
          assessmentId: _assessmentId,
          questionGroupId: assessmentResult?.questionGroupId ?? '',
          userName: _username,
          progressStatus: '',
          record: _buildQuestionRecordList()));
    } catch (e) {
      Log.debug(e);
    } finally {
      Future.delayed(const Duration(milliseconds: 500), () {
        EasyLoading.dismiss();
      });
    }
  }

  List<Map> _buildQuestionRecordList() {
    List<Map> questionRecordList = [];
    AssessmentResult? result = assessmentResult;
    var partDataList = result!.list;
    //Part
    // ignore: unused_local_variable
    int questionStepIndex = -1;
    // ignore: unused_local_variable
    int questionStepNum = -1;
    for (var pIndex = 0; pIndex < partDataList!.length; pIndex++) {
      var partData = partDataList[pIndex];
      var sectionDataList = partData.questions;
      questionStepIndex++;
      //Section
      for (var sIndex = 0; sIndex < sectionDataList!.length; sIndex++) {
        var sectionData = sectionDataList[sIndex];
        var questionDataList = sectionData.question;
        //Question
        for (var qIndex = 0; qIndex < questionDataList!.length; qIndex++) {
          QuestionItem questionData = questionDataList[qIndex];

          questionStepNum++;
          questionStepIndex++;
          questionRecordList.add(QuestionSingleStep(
                  answer: questionData.answer,
                  number: questionData.number,
                  questionId: sectionData.id)
              .toJson());
        }
      }
    }

    return questionRecordList;
  }

  @override
  void onInit() {
    pdfViewerController = PdfViewerController();
    super.onInit();
  }

  @override
  void onClose() {
    BGSinglePlayer().stop();
    super.onClose();
  }
}
